import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";
import { useUserStore } from "../store/user.js";
const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/course",
    name: "Course",
    component: () =>
      import(/* webpackChunkName: "course" */ "../views/Course.vue"),
  },
  {
    path: "/course-info/:id",
    name: "CourseInfo",
    component: () =>
      import(/* webpackChunkName: "courseInfo" */ "../views/CourseInfo.vue"),
  },
  { 
    path:'/course-play/:courseId/:chapterId',
    name:'Course-play',
    component: () =>
      import(/* webpackChunkName: "CoursePlay" */ "../views/CoursePlay.vue"),
},
  {
    path: "/login",
    name: "Login",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/Login.vue"),
  },
  {
    path: "/cart",
    name: "Cart",
    component: () =>
      import(/* webpackChunkName: "cart" */ "../views/Cart.vue"),
      beforeEnter: (to, from, next) => {
        // ...
        if(useUserStore().userInfo.id) {
          next()
        }else {
          next('/login')
        }
      }
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;